#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
简化版足球赛事分析对口型视频生成脚本
根据JSON格式的对话生成完整的对口型视频
"""

import sys
import os
import json
from pathlib import Path

# 添加项目根目录到 Python 路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

from src.processors.lip_sync_generator import LipSyncGenerator
from src.processors.wavespeed_ai import WaveSpeedAI


def main():
    """主函数"""
    print("开始生成足球赛事分析对口型视频")
    print("=" * 60)
    
    # 检查API密钥
    elevenlabs_api_key = os.getenv("ELEVENLABS_API_KEY")
    wavespeed_api_key = os.getenv("WAVESPEED_API_KEY")
    
    if not elevenlabs_api_key:
        print("警告: 未设置 ELEVENLABS_API_KEY 环境变量")
        print("请设置该环境变量以生成音频")
        return
    
    if not wavespeed_api_key:
        print("警告: 未设置 WAVESPEED_API_KEY 环境变量")
        print("请设置该环境变量以生成视频")
        return
    
    try:
        # 初始化处理器
        generator = LipSyncGenerator()
        wavespeed_ai = WaveSpeedAI()
        
        # 创建足球赛事分析对话（葡萄牙语-巴西）
        # 对话简短，控制在30秒内
        json_input = {
            "background_image": None,
            "dialogue": [
                {
                    "speaker": "left", 
                    "text": "E aí galera! Hoje o grande jogo é Palmeiras contra Corinthians."
                },
                {
                    "speaker": "right", 
                    "text": "O Palmeiras tá mandando muito bem, principalmente em casa."
                },
                {
                    "speaker": "left", 
                    "text": "Mas o Corinthians tem uma defesa muito forte, pode complicar."
                },
                {
                    "speaker": "right", 
                    "text": "Meu palpite: 2 a 1 para o Palmeiras. O que vocês acham?"
                }
            ]
        }
        
        print("输入对话内容 (葡萄牙语-巴西):")
        for i, turn in enumerate(json_input["dialogue"]):
            print(f"  {i+1}. {turn['speaker']}: {turn['text']}")
        
        # 生成音频和图片
        print("\n正在生成音频和背景图片...")
        result = generator.process_json_input(json_input)
        
        if not result["success"]:
            print("音频和图片生成失败!")
            print(f"错误信息: {result.get('error', '未知错误')}")
            return
        
        print("✓ 音频和图片生成成功!")
        print(f"输出文件夹: {result['folder_path']}")
        
        # 生成对口型视频
        print("\n正在生成对口型视频...")
        video_url = wavespeed_ai.generate_lipsync_video_from_folder(
            folder_path=result['folder_path'],
            order="meanwhile",
            resolution="480p"
        )
        
        if video_url:
            print("✓ 对口型视频生成成功!")
            print(f"视频URL: {video_url}")
        else:
            print("✗ 对口型视频生成失败!")
            
    except Exception as e:
        print(f"生成过程中出现错误: {e}")
        import traceback
        traceback.print_exc()
    
    print("\n" + "=" * 60)
    print("视频生成完成")


if __name__ == "__main__":
    main()